﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.CRL.Tools;
using System.IO;
using Empires.CRL.DataContract;
using System.Web;
using System.Text.RegularExpressions;

namespace Empires.BLL.ServerProvider
{
    public static class CompanyHelper
    {
        /// <summary>
        /// 创建基础公司私有文件夹
        /// 包括appSetting中配置的CustomerFile,CustomerImage,CustomerScript
        /// </summary>
        /// <param name="copName"></param>
        public static void CreateCompanyDirectory(string copName)
        {
            string filedirector = String.Format(Utils.ReadAppSetting("CustomerFile"), copName);
            if (!Directory.Exists(filedirector))
            {
                Directory.CreateDirectory(filedirector);
            }
            string imagedirector = String.Format(Utils.ReadAppSetting("CustomerImage"), copName);
            if (!Directory.Exists(imagedirector))
            {
                Directory.CreateDirectory(imagedirector);
            }
            string scriptdirector = String.Format(Utils.ReadAppSetting("CustomerScript"), copName);
            if (!Directory.Exists(scriptdirector))
            {
                Directory.CreateDirectory(scriptdirector);
            }
        }

        public static List<DefaultDataPack> InjuectPagingInfo(Func<int, List<DefaultDataPack>> func, int itemCount, int currentPage, int take)
        {
            int pagecount = itemCount % take == 0 ? itemCount / take : itemCount / take + 1;
            if (pagecount > 0)
            {
                currentPage = currentPage < 0 ? 0 : currentPage;
                currentPage = currentPage > pagecount - 1 ? pagecount - 1 : currentPage;
            }
            else
            {
                currentPage = 0;
            }
            int previouspage = currentPage == 0 ? 0 : currentPage - 1;
            int nextpage = currentPage == pagecount - 1 ? pagecount - 1 : currentPage + 1;
            var data = func.Invoke(currentPage);
            return data = data.Select(c =>
            {
                c.ITEMCOUNT = itemCount;
                c.PAGESIZE = take;
                c.CURRENTPAGE = currentPage + 1;
                c.PAGECOUNT = pagecount;
                c.PREVIOUSPAGE = previouspage;
                c.NEXTPAGE = nextpage;
                c.FIRSTPAGE = 0;
                c.LASTPAGE = pagecount - 1;
                return c;
            }).ToList();
        }

    }
}
